const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    courses: [
      {
        id: 1,
        name: '计算机基础知识',
        description: '计算机二级考试全国计算机等级考试',
        price: '1000.00',
        image: '/images/course1.png'
      },
      {
        id: 2,
        name: '电子电路EDA技术',
        description: '汉语言文学',
        price: '100.00',
        image: '/images/course2.png'
      }
    ],
    hotCourses: [], // 热门课程数据
    textCourses: [
      {
        id: 1,
        title: '考研高数',
        desc: '《考研高数》共分10...',
        image: '/images/考研高数.png',
        price: '1500.00'
      }
    ],
    videoCourses: [
      {
        id: 1,
        title: '单片机嵌入式原理与技术',
        desc: '单片机嵌入式原理与技术...',
        image: '/images/单片机嵌入式原理与技术.png',
        price: '0.00',
        tag: '免费'
      }
    ],
    bannerList: [ // 轮播图数据
      { image: '/images/banner1.png' },
      { image: '/images/banner2.png' }
    ]
  },

  onLoad() {
    this.fetchHotCourses(); // 页面加载时获取热门课程数据
  },

  // 获取热门课程数据
  fetchHotCourses() {
    wx.request({
      url: 'http://localhost:8080/courses/findAll',
      method: 'GET',
      success: (res) => {
        const hotCourses = res.data.slice(0, 2); // 获取前两条数据
        this.setData({ hotCourses });
      },
      fail: (err) => {
        console.error('Failed to fetch hot courses:', err);
      }
    });
  },

  // 用户信息相关方法
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    });
  },

  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    const { nickName } = this.data.userInfo;
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    });
  },

  onInputChange(e) {
    const nickName = e.detail.value;
    const { avatarUrl } = this.data.userInfo;
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    });
  },

  getUserProfile(e) {
    wx.getUserProfile({
      desc: '展示用户信息',
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        });
      }
    });
  },

  // 搜索功能
  onSearch: function (e) {
    const keyword = e.detail.value;
    wx.showToast({
      title: `搜索: ${keyword}`,
      icon: 'none'
    });
  },

  // 导航方法
  onPracticeClick() {
    wx.navigateTo({
      url: '/pages/practice/practice'
    });
  },

  onStudyClick() {
    wx.navigateTo({
      url: '/pages/study/study'
    });
  },

  onScoreClick() {
    wx.navigateTo({
      url: '/pages/score/score'
    });
  },

  onNoticeClick() {
    wx.navigateTo({
      url: '/pages/notice/notice'
    });
  }
});
